<?php
namespace Home\Controller;
use Lib\Alidayu\SendSMS;
class InsideController extends CommonController {

	public function _initialize()
	{
		$this->Is_login();        //检测用户登录信息,如果错误直接返回错误,终止程序
		$this->Is_power("inside");        //检测用户权限,如果错误直接返回错误,终止程序
		$this->assign('heardadminData',session('heardadminData'));
	}

	//添加内部账号列表
	public function inside_member_add(){
		$this->p = I('get.p')?I('get.p'):1;
		$user_name = I('get.user_name');
		$this->user_name = $user_name;
		$where['inside'] = 1;
		if(!empty($user_name)){
			$where['username'] = ['like',"%$user_name%"];
			$count = M('member m')->where(array('status'=>9))->field('m.*')->where($where)->join('__WECHAT__ w on w.uuid = m.uuid','left')->count();
			$p = getpage($count, 20);
			$userinfo = M('member m')->where(array('status'=>9))->field('m.*')->where($where)->join('__WECHAT__ w on w.uuid = m.uuid','left')->limit($p->firstRow, $p->listRows)->select();
			$leveldata = M("systemsetup")->where(array("type"=>1))->order('value asc')->select();

			foreach ($userinfo as $key2 => &$value2) {
				foreach ($leveldata as $k => $v) {
					if ($value2['level'] >= $v['value']) {
						$value2['level_type'] = $v['message'];
					}
				}
	//			$value2['invitecount'] = M('invite')->where(array('inviteid'=>$value2['uuid']))->count();
				// 已完成订单数量
	//			$value2['ordernum'] = M('order')->field('id')->where(array('user_id'=>$value2['uuid'],'status'=>8))->count();
				// 邀请人
				$inviteid = M('invite')->field('inviteid')->where(array('uuid'=>$value2['uuid']))->find()['inviteid'];
				$value2['invite_nickname'] = M('member m')->where(array('uuid'=>$inviteid))->field('nickname')->find()['nickname'];
				if (empty($value2['invite_nickname'])) {
					$value2['invite_nickname'] = "无";
				}
				$value2['openid'] = M('wechat')->where(array('uuid'=>$value2['uuid']))->field('openid')->find()['openid'];
			}
			$page = $p->show();
			$map['user_name'] = empty($user_name)?"":$user_name;
			foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
			}
			$this->assign('userinfo',$userinfo);
			$this->assign('page', $page);
		}
		$this->display();
	}

	//ajax添加内部账号
	public function add_member_inside(){
		$uuid = I('post.uuid');
		if(empty($uuid)){
			$this->ApiError('参数错误','888888');
		}
		$member_info = M('member')->where(['uuid'=>$uuid,'status'=>9])->find();
		if(!$member_info){
			$this->ApiError('账号有误','888888');
		}
		$save['inside'] = 2;
		$res = M('member')->where(['uuid'=>$uuid])->save($save);
		if($res){
			$this->adminlog("添加主播内部账号".$uuid);
			$this->ApiSuccess('操作成功');
		}else{
			$this->ApiError('操作失败','999999');
		}
	}

	//主播内部账号列表
	public function inside_member_list(){
		$this->p = I('get.p')?I('get.p'):1;
		$user_name = I('get.user_name');
		$this->user_name = $user_name;
		$where['inside'] = 2;
		if(!empty($user_name)){$where['username'] = ['like',"%$user_name%"];}
		$count = M('member m')->where(array('status'=>9))->field('m.*')->where($where)->join('__WECHAT__ w on w.uuid = m.uuid','left')->count();
		$p = getpage($count, 20);
		$userinfo = M('member m')->where(array('status'=>9))->field('m.*')->where($where)->join('__WECHAT__ w on w.uuid = m.uuid','left')->limit($p->firstRow, $p->listRows)->select();
		$leveldata = M("systemsetup")->where(array("type"=>1))->order('value asc')->select();

		foreach ($userinfo as $key2 => &$value2) {
			foreach ($leveldata as $k => $v) {
				if ($value2['level'] >= $v['value']) {
					$value2['level_type'] = $v['message'];
				}
			}
//			$value2['invitecount'] = M('invite')->where(array('inviteid'=>$value2['uuid']))->count();
			// 已完成订单数量
//			$value2['ordernum'] = M('order')->field('id')->where(array('user_id'=>$value2['uuid'],'status'=>8))->count();
			// 邀请人
			$inviteid = M('invite')->field('inviteid')->where(array('uuid'=>$value2['uuid']))->find()['inviteid'];
			$value2['invite_nickname'] = M('member m')->where(array('uuid'=>$inviteid))->field('nickname')->find()['nickname'];
			if (empty($value2['invite_nickname'])) {
				$value2['invite_nickname'] = "无";
			}
			$value2['openid'] = M('wechat')->where(array('uuid'=>$value2['uuid']))->field('openid')->find()['openid'];
		}
		$page = $p->show();
		$map['user_name'] = empty($user_name)?"":$user_name;
		foreach($map as $key=>$val) {
			$p->parameter .= "$key=".urlencode($val)."&";
		}
		$this->assign('userinfo',$userinfo);
		$this->assign('page', $page);
		$this->display();
	}

	//ajax移除内部账号
	public function inside_member_del(){
		$uuid = I('post.uuid');
		if(empty($uuid)){
			$this->ApiError('参数错误','888888');
		}
		$member_info = M('member')->where(['uuid'=>$uuid,'status'=>9])->find();
		if(!$member_info){
			$this->ApiError('账号有误','888888');
		}
		$save['inside'] = 1;
		$res = M('member')->where(['uuid'=>$uuid])->save($save);
		if($res){
			$this->adminlog("移除主播内部账号".$uuid);
			$this->ApiSuccess('操作成功');
		}else{
			$this->ApiError('操作失败','999999');
		}
	}

	//添加内部账号列表
	public function inside_wemedia_add()
	{
		$db_wemedia = M("wemedia");
		$user_name = I('get.user_name');
		$where = " 1=1 and status=2 and inside=1";
		if (!empty($user_name)) {$where .= " and (username like '%$user_name%')";
			$count = $db_wemedia->where($where)->count();
			$p = getpage($count, 20);
			$wemedialist = $db_wemedia->where($where)->field("password",true)->limit($p->firstRow, $p->listRows)->select();
			foreach ($wemedialist as $key => &$value) {
				$value['qianyue'] = '';
				$value['weiqianyue'] = '';
				$media_list = M('media')->field('media_name,uuid')->where(['wid'=>$value['wid']])->select();
				foreach ($media_list as $v){
					if($v['uuid']==0){
						$value['weiqianyue'] .= $v['media_name'].',';
					}else{
						$value['qianyue'] .= $v['media_name'].',';
					}
				}
				$value['qianyue'] = trim($value['qianyue']);
				$value['weiqianyue'] = trim($value['weiqianyue']);
			}
			$page = $p->show();
			// 获取查询参数
			$map['user_name'] = I('get.user_name');
			$p->parameter = "";
			foreach($map as $key=>$val) {
				$p->parameter .= "$key=".urlencode($val)."&";
			}
			// p($wemedialist);exit();
			$this->assign('wemedialist',$wemedialist);
			$this->assign('page', $page);
		}
		$this->display();
	}

	//ajax添加内部自媒体账号
	public function add_wemedia_inside(){
		$wid = I('post.wid');
		if(empty($wid)){
			$this->ApiError('参数错误','888888');
		}
		$member_info = M('wemedia')->where(['wid'=>$wid,'status'=>2])->find();
		if(!$member_info){
			$this->ApiError('账号有误','888888');
		}
		$save['inside'] = 2;
		$res = M('wemedia')->where(['wid'=>$wid])->save($save);
		if($res){
			$this->adminlog("添加自媒体内部账号".$wid);
			$this->ApiSuccess('操作成功');
		}else{
			$this->ApiError('操作失败','999999');
		}
	}

	//账号内部列表
	public function inside_wemedia_list()
	{
		$db_wemedia = M("wemedia");
		$user_name = I('get.user_name');
		$where = " 1=1 and status=2 and inside=2";
		if (!empty($user_name)) {$where .= " and (username like '%$user_name%')";}
		$count = $db_wemedia->where($where)->count();
		$p = getpage($count, 20);
		$wemedialist = $db_wemedia->where($where)->field("password",true)->limit($p->firstRow, $p->listRows)->select();
		foreach ($wemedialist as $key => &$value) {
			$value['qianyue'] = '';
			$value['weiqianyue'] = '';
			$media_list = M('media')->field('media_name,uuid')->where(['wid'=>$value['wid']])->select();
			foreach ($media_list as $v){
				if($v['uuid']==0){
					$value['weiqianyue'] .= $v['media_name'].',';
				}else{
					$value['qianyue'] .= $v['media_name'].',';
				}
			}
			$value['qianyue'] = trim($value['qianyue']);
			$value['weiqianyue'] = trim($value['weiqianyue']);
		}
		$page = $p->show();
		// 获取查询参数
		$map['user_name'] = I('get.user_name');
		$p->parameter = "";
		foreach($map as $key=>$val) {
			$p->parameter .= "$key=".urlencode($val)."&";
		}
		// p($wemedialist);exit();
		$this->assign('wemedialist',$wemedialist);
		$this->assign('page', $page);
		$this->display();
	}

	//ajax移除自媒体内部账号
	public function inside_wemedia_del(){
		$wid = I('post.wid');
		if(empty($wid)){
			$this->ApiError('参数错误','888888');
		}
		$member_info = M('wemedia')->where(['wid'=>$wid,'status'=>2])->find();
		if(!$member_info){
			$this->ApiError('账号有误','888888');
		}
		$save['inside'] = 1;
		$res = M('wemedia')->where(['wid'=>$wid])->save($save);
		if($res){
			$this->adminlog("移除自媒体内部账号".$wid);
			$this->ApiSuccess('操作成功');
		}else{
			$this->ApiError('操作失败','999999');
		}
	}
}